package com.example.mvp.mvppromote.base;

import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.example.mvp.mvppromote.R;
import com.example.mvp.mvppromote.utils.AppManager;
import com.example.mvp.mvppromote.utils.StatusBarUtil;

import butterknife.ButterKnife;

/**
 * Created by wty on 2016/6/23 0023.
 */
public abstract class BaseActivity<T extends IPresenter> extends AppCompatActivity {
    protected T mPresenter;

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        boolean animition = intent.getBooleanExtra("animition", true);
        if (animition) {
            overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_right_reman);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        AppManager.getAppManager().addActivity(this);
        mPresenter = getPresenter();
        initData();
    }

    @Override
    public void finish() {
        super.finish();
        boolean animition = getIntent().getBooleanExtra("animition", true);
        if (animition) {
            overridePendingTransition(0, R.anim.base_slide_right_out);
        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {

        super.startActivityForResult(intent, requestCode);
        boolean animition = intent.getBooleanExtra("animition", true);
        if (animition) {
            overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_right_reman);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unsubcrible();
        }
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.base_slide_right_out);
    }

    public int setToolBar(FloatingActionButton floatingActionButton, Toolbar toolbar, boolean isChangeToolbar, boolean isChangeStatusBar, DrawerLayout drawerLayout) {
        int vibrantColor = 0;
        int mutedColor = 0;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(Color.BLACK);
        }
        if (floatingActionButton != null)
            floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(mutedColor));
        if (isChangeToolbar)
            toolbar.setBackgroundColor(vibrantColor);
        if (isChangeStatusBar) {
            StatusBarUtil.setColor(this, vibrantColor);
        }
        if (drawerLayout != null) {
            StatusBarUtil.setColorForDrawerLayout(this, drawerLayout, vibrantColor);
        }
        return vibrantColor;
    }


    protected abstract T getPresenter();

    protected abstract int getLayoutId();

    protected abstract void initData();

}
